﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;

namespace produccion.forms
{
    /// <summary>
    /// Lógica de interacción para FRMReporteProcProd.xaml
    /// </summary>
    public partial class FRMReporteProcProd : Window
    {
        MDOgeneral _mod = new MDOgeneral();

        public FRMReporteProcProd()
        {
            InitializeComponent();
        }

        private void ReporteProcProd_Loaded(object sender, RoutedEventArgs e)
        {
            #region EstilosParaDataGrid
            Style MyStyleTextAlignmentProperty = new Style(typeof(DataGridCell));
            MyStyleTextAlignmentProperty.Setters.Add(new Setter(TextBlock.TextAlignmentProperty, TextAlignment.Right));

            dtgTallas.Columns[1].CellStyle = MyStyleTextAlignmentProperty;
            dtgTallas.Columns[2].CellStyle = MyStyleTextAlignmentProperty;
            dtgTallas.Columns[3].CellStyle = MyStyleTextAlignmentProperty;
            dtgTallas.Columns[4].CellStyle = MyStyleTextAlignmentProperty;
            dtgTallas.Columns[5].CellStyle = MyStyleTextAlignmentProperty;
            dtgTallas.Columns[6].CellStyle = MyStyleTextAlignmentProperty;
            dtgTallas.Columns[7].CellStyle = MyStyleTextAlignmentProperty;
            dtgTallas.Columns[8].CellStyle = MyStyleTextAlignmentProperty;
            dtgTallas.Columns[9].CellStyle = MyStyleTextAlignmentProperty;
            dtgTallas.Columns[10].CellStyle = MyStyleTextAlignmentProperty;
            dtgTallas.Columns[11].CellStyle = MyStyleTextAlignmentProperty;
            dtgTallas.Columns[12].CellStyle = MyStyleTextAlignmentProperty;
            dtgTallas.Columns[13].CellStyle = MyStyleTextAlignmentProperty;
            dtgTallas.Columns[14].CellStyle = MyStyleTextAlignmentProperty;
            dtgTallas.Columns[15].CellStyle = MyStyleTextAlignmentProperty;
            dtgTallas.Columns[16].CellStyle = MyStyleTextAlignmentProperty;
            dtgTallas.Columns[17].CellStyle = MyStyleTextAlignmentProperty;
            dtgTallas.Columns[18].CellStyle = MyStyleTextAlignmentProperty;
            dtgTallas.Columns[19].CellStyle = MyStyleTextAlignmentProperty;
            dtgTallas.Columns[20].CellStyle = MyStyleTextAlignmentProperty;


            dtgOperario.Columns[2].CellStyle = MyStyleTextAlignmentProperty;
            #endregion

            txtNumeroOrden.Style = null;//FJRF - Quitamos el estilo para que se pueda personalizar el manejo del foco
            txtTotalaReportar.Style = null;//FJRF - Quitamos el estilo para que se pueda personalizar el manejo del foco

            Keyboard.Focus(txtOpcion);
        }


        //
        // SINGLE CLICK EDITING
        //
        private void DataGridCell_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            DataGridCell cell = sender as DataGridCell;
            if (cell != null && !cell.IsEditing && !cell.IsReadOnly)
            {
                if (!cell.IsFocused)
                {
                    cell.Focus();
                }
                DataGrid dataGrid = FindVisualParent<DataGrid>(cell);
                if (dataGrid != null)
                {
                    if (dataGrid.SelectionUnit != DataGridSelectionUnit.FullRow)
                    {
                        if (!cell.IsSelected)
                            cell.IsSelected = true;
                    }
                    else
                    {
                        DataGridRow row = FindVisualParent<DataGridRow>(cell);
                        if (row != null && !row.IsSelected)
                        {
                            row.IsSelected = true;
                        }
                    }
                }
            }
        }
        static T FindVisualParent<T>(UIElement element) where T : UIElement
        {
            UIElement parent = element;
            while (parent != null)
            {
                T correctlyTyped = parent as T;
                if (correctlyTyped != null)
                {
                    return correctlyTyped;
                }

                parent = VisualTreeHelper.GetParent(parent) as UIElement;
            }
            return null;
        }

        private void ReporteProcProd_KeyDown(object sender, KeyEventArgs e)
        {
            var elemento = e.OriginalSource as UIElement;
            if (e.Key == Key.F3 && elemento != null)
            {
                if (MDOgeneral.logBusquedadAbierta == true) { MDOgeneral.logBusquedadAbierta = false; }
            }

            if (e.Key == Key.F4 && elemento != null)
            {
                if (MDOgeneral.logBusquedadAbierta1 == true) { MDOgeneral.logBusquedadAbierta1 = false; }
            }

            if (e.Key == Key.F5 && elemento != null)
            {
                if (MDOgeneral.logBusquedadAbierta2 == true) { MDOgeneral.logBusquedadAbierta2 = false; }
            }
        }

        private void dtgOperario_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            #region FJRF - Saber Fila y Columna indexada

            int IntColumnaIndexada, IntFilaIndexada;
            DependencyObject dep = (DependencyObject)e.OriginalSource;

            //Stepping through the visual tree
            while ((dep != null) && !(dep is System.Windows.Controls.DataGridCell))
            {
                dep = VisualTreeHelper.GetParent(dep);
            }

            //Is the dep a cell or outside the bounds of Window1?
            if (dep == null | !(dep is System.Windows.Controls.DataGridCell))
            {
                return;
            }
            else
            {
                System.Windows.Controls.DataGridCell cell = new System.Windows.Controls.DataGridCell();
                cell = (System.Windows.Controls.DataGridCell)dep;
                while ((dep != null) && !(dep is DataGridRow))
                {
                    dep = VisualTreeHelper.GetParent(dep);
                }

                if (dep == null)
                {
                    return;
                }
                int colindex = cell.Column.DisplayIndex;  //this returns COLUMN INDEX
                IntColumnaIndexada = colindex;

                DataGridRow row = dep as DataGridRow;
                System.Windows.Controls.DataGrid dataGrid = ItemsControl.ItemsControlFromItemContainer(row) as System.Windows.Controls.DataGrid;
                int rowindex = dataGrid.ItemContainerGenerator.IndexFromContainer(row); //this returns ROW INDEX

                IntFilaIndexada = rowindex;
            }
            #endregion

            var elemento = e.OriginalSource as UIElement;

            if (e.Key == Key.Enter && elemento != null)
            {
                if (IntColumnaIndexada == 2)//FJRF - Si la columna indexada es el porcentaje entonces hacemos lo siguiente
                {
                    txtTotalaReportar.Focus();//FJRF - Pasamos el foco para que reporten
                }
                else//FJRF - Sino es la columna que necesitamos que pase a la siguiente columna
                {
                    e.Handled = true;
                    elemento.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
                }
            }
        }

        private void dtgTallas_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            var elemento = e.OriginalSource as UIElement;
            if (e.Key == Key.Enter && elemento != null)
            {
                e.Handled = true;
                elemento.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
            }
        }

        private void txtNumeroOrden_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                if (txtNumeroOrden.Text.Trim().Length == 6)
                {
                    string query = "select a.codtal_tal, a.postal_tal from taltal a inner join maeref b on a.codtje_tal=b.talref_ref where b.codref_ref='" + txbCodRef.Text.Trim() + "' order by a.codtje_tal, a.postal_tal ";
                    DataTable val = _mod.consulta(query);
                    int numRegistro = 0;
                    int numtotal = val.Rows.Count;
                    foreach (DataRow dr in val.Rows)
                    {
                        if (numRegistro == 0 && numtotal > 0)
                            dtgTallas.Columns[1].Header = dr[0].ToString();

                        if (numRegistro == 1 && numtotal > 1)
                            dtgTallas.Columns[2].Header = dr[0].ToString();

                        if (numRegistro == 2 && numtotal > 2)
                            dtgTallas.Columns[3].Header = dr[0].ToString();

                        if (numRegistro == 3 && numtotal > 3)
                            dtgTallas.Columns[4].Header = dr[0].ToString();

                        if (numRegistro == 4 && numtotal > 4)
                            dtgTallas.Columns[5].Header = dr[0].ToString();

                        if (numRegistro == 5 && numtotal > 5)
                            dtgTallas.Columns[6].Header = dr[0].ToString();

                        if (numRegistro == 6 && numtotal > 6)
                            dtgTallas.Columns[7].Header = dr[0].ToString();

                        if (numRegistro == 7 && numtotal > 7)
                            dtgTallas.Columns[8].Header = dr[0].ToString();

                        if (numRegistro == 8 && numtotal > 8)
                            dtgTallas.Columns[9].Header = dr[0].ToString();

                        if (numRegistro == 9 && numtotal > 9)
                            dtgTallas.Columns[10].Header = dr[0].ToString();
                        
                        if (numRegistro == 10 && numtotal > 10)
                            dtgTallas.Columns[11].Header = dr[0].ToString();

                        if (numRegistro == 11 && numtotal > 11)
                            dtgTallas.Columns[12].Header = dr[0].ToString();

                        if (numRegistro == 12 && numtotal > 12)
                            dtgTallas.Columns[13].Header = dr[0].ToString();

                        if (numRegistro == 13 && numtotal > 13)
                            dtgTallas.Columns[14].Header = dr[0].ToString();

                        if (numRegistro == 14 && numtotal > 14)
                            dtgTallas.Columns[15].Header = dr[0].ToString();

                        if (numRegistro == 15 && numtotal > 15)
                            dtgTallas.Columns[16].Header = dr[0].ToString();

                        if (numRegistro == 16 && numtotal > 16)
                            dtgTallas.Columns[17].Header = dr[0].ToString();

                        if (numRegistro == 17 && numtotal > 17)
                            dtgTallas.Columns[18].Header = dr[0].ToString();

                        if (numRegistro == 18 && numtotal > 18)
                            dtgTallas.Columns[19].Header = dr[0].ToString();

                        if (numRegistro == 19 && numtotal > 19)
                            dtgTallas.Columns[20].Header = dr[0].ToString();

                        numRegistro = numRegistro + 1;
                    }

                    if (numtotal == 10)
                    {
                        dtgTallas.Columns[20].Header = "";
                        dtgTallas.Columns[19].Header = "";
                        dtgTallas.Columns[18].Header = "";
                        dtgTallas.Columns[17].Header = "";
                        dtgTallas.Columns[16].Header = "";
                        dtgTallas.Columns[15].Header = "";
                        dtgTallas.Columns[14].Header = "";
                        dtgTallas.Columns[13].Header = "";
                        dtgTallas.Columns[12].Header = "";
                        dtgTallas.Columns[11].Header = "";
                    }
                    if (numtotal == 11)
                    {
                        dtgTallas.Columns[20].Header = "";
                        dtgTallas.Columns[19].Header = "";
                        dtgTallas.Columns[18].Header = "";
                        dtgTallas.Columns[17].Header = "";
                        dtgTallas.Columns[16].Header = "";
                        dtgTallas.Columns[15].Header = "";
                        dtgTallas.Columns[14].Header = "";
                        dtgTallas.Columns[13].Header = "";
                        dtgTallas.Columns[12].Header = "";
                    }
                    if (numtotal == 12)
                    {
                        dtgTallas.Columns[20].Header = "";
                        dtgTallas.Columns[19].Header = "";
                        dtgTallas.Columns[18].Header = "";
                        dtgTallas.Columns[17].Header = "";
                        dtgTallas.Columns[16].Header = "";
                        dtgTallas.Columns[15].Header = "";
                        dtgTallas.Columns[14].Header = "";
                        dtgTallas.Columns[13].Header = "";
                    }
                    if (numtotal == 13)
                    {
                        dtgTallas.Columns[20].Header = "";
                        dtgTallas.Columns[19].Header = "";
                        dtgTallas.Columns[18].Header = "";
                        dtgTallas.Columns[17].Header = "";
                        dtgTallas.Columns[16].Header = "";
                        dtgTallas.Columns[15].Header = "";
                        dtgTallas.Columns[14].Header = "";
                    }
                    if (numtotal == 14)
                    {
                        dtgTallas.Columns[20].Header = "";
                        dtgTallas.Columns[19].Header = "";
                        dtgTallas.Columns[18].Header = "";
                        dtgTallas.Columns[17].Header = "";
                        dtgTallas.Columns[16].Header = "";
                        dtgTallas.Columns[15].Header = "";
                    }
                    if (numtotal == 15)
                    {
                        dtgTallas.Columns[20].Header = "";
                        dtgTallas.Columns[19].Header = "";
                        dtgTallas.Columns[18].Header = "";
                        dtgTallas.Columns[17].Header = "";
                        dtgTallas.Columns[16].Header = "";
                    }
                    if (numtotal == 16)
                    {
                        dtgTallas.Columns[20].Header = "";
                        dtgTallas.Columns[19].Header = "";
                        dtgTallas.Columns[18].Header = "";
                        dtgTallas.Columns[17].Header = "";
                    }
                    if (numtotal == 17)
                    {
                        dtgTallas.Columns[20].Header = "";
                        dtgTallas.Columns[19].Header = "";
                        dtgTallas.Columns[18].Header = "";
                    }
                    if (numtotal == 18)
                    {
                        dtgTallas.Columns[20].Header = "";
                        dtgTallas.Columns[19].Header = "";
                    }
                    if (numtotal == 19)
                    {
                        dtgTallas.Columns[20].Header = "";
                    }

                }
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void btnGrabar_Click(object sender, RoutedEventArgs e)
        {
            Keyboard.Focus(txtOpcion);
        }

        private void txtNumeroOrden_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)//FJRF - Si el usuario digita enter entonces hacemos lo siguiente
            {
                dtgOperario.CurrentCell = new DataGridCellInfo(dtgOperario.Items[0], dtgOperario.Columns[0]);//FJRF - Decimos en que fila y columna vamos a trabajar
                dtgOperario.BeginEdit();//FJRF - Decimos que es para edicion
            }
        }

        private void txtTotalaReportar_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                if (btnGrabar.IsEnabled == true)
                {
                    btnGrabar.Focus();
                }
            }
        }

    }
}
